<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>全选反选</title>
  <script src="../vue.js"></script>
</head>

<body>
  <div id="app">
    <div>
      <span>全选:</span>
      <input v-model="isAll" type="checkbox" />
      <button @click="reverseBtn">反选</button>
      <ul>
        <li v-for="(item, index) in arr" :key="index">
          <input v-model="item.checked" type="checkbox" />
          <span>{{item.name}}</span>
        </li>
      </ul>
    </div>
  </div>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        arr: [
          {
            name: "猪八戒",
            checked: false,
          },
          {
            name: "孙悟空",
            checked: false,
          },
          {
            name: "唐僧",
            checked: false,
          },
          {
            name: "白龙马",
            checked: false,
          },
        ],
      },
      methods: {
        reverseBtn() {
          this.isAll = !this.isAll
        }
      },
      computed: {
        isAll: {
          get() {
            console.log(1);
            return this.arr.every(item => item.checked)
          },
          set(newVal) {
            console.log(newVal);
            this.arr.forEach(item => item.checked = newVal);
          },
        }
      },
      watch: {

      }
    })
  </script>
</body>

</html>